package com.tn.wservice.validator;

import org.springframework.util.StringUtils;

import com.tn.bo.Company;
import com.tn.bo.LoginData;
import com.tn.bo.UserLoginDetails;
import com.tn.bo.UserProfile;
import com.tn.dao.CompanyDAO;
import com.tn.dao.UserDAO;
import com.tn.wservice.validator.IValidationResult.Result;

public class LoginValidator implements IValidator {

	@Override
	public IValidationResult validate(Object businessObject) throws Exception {
		LoginValidationResult result = new LoginValidationResult();

		if (businessObject instanceof LoginData) {
			LoginData loginData = (LoginData) businessObject;

			String username = loginData.getUsername();
			String password = loginData.getPassword();
			if (username == null || !StringUtils.hasText(username)) {
				result.setResult(Result.FAILURE);
				result.addErrorMsg("Username can not be blank");
			}

			if (password == null || !StringUtils.hasText(password)) {
				result.setResult(Result.FAILURE);
				result.addErrorMsg("Password can not be blank");
			}

			if (result.getResult() == Result.FAILURE) {
				return result;
			} else {
				boolean auth = false;
				String loginFailReason = "";
				try {
					UserDAO uDao = new UserDAO();
					UserLoginDetails details = uDao.findUserByUserId(username);

					if (details == null) {
						loginFailReason = "User " + username+ " does not exist.";
					}else{
						details.setPlainTextPassword(password);
						auth = UserLoginDetails.isAuthenticated(details);
						if (!auth) {
							loginFailReason = "Password is incorrect";
						} else {
							UserProfile userProfile = uDao.getUserProfile(details.getUserProfileId());
							CompanyDAO cDao = new CompanyDAO();
							if (userProfile.getCompanyId() != null) {
								Company company = cDao.findCompanyByCompanyId(userProfile.getCompanyId());
								if (company == null) {
									auth = false;
									loginFailReason = "Company Information for User " + username + " not found. Please register again.";
								} else {
									result.setUserName(details.getUserId());
									result.setUserProfile(userProfile);
									result.setUserCompany(company);
									
									if (company.getCompanyProfileAdmins() != null
											&& company.getCompanyProfileAdmins().contains(userProfile.getUserProfileId().toString())) {
										result.setCompanyAdmin(true);
									} else {
										if ("false".equalsIgnoreCase(userProfile.getIsVerified())) {
											auth = false;
											loginFailReason = "Approval is Pending From Company Admin";
										} else {
											result.setCompanyAdmin(false);
										}
									}
								}
							} else {
								auth = false;
								loginFailReason = "Authorize yourself from your Email.Click on the link sent to you by mail ";
							}
						}
					}
				} catch (Exception e) {
					e.printStackTrace();
					auth = false;
					loginFailReason = "Login Service not available. Please try again after some time.";
				}
				if(!auth){
					result.setResult(Result.FAILURE);
					result.addErrorMsg(loginFailReason);
				}
				
				return result;
			}
		} else {
			throw new IllegalArgumentException(
					"LoginData can validate only LoginData objects. Invalid object configured :"
							+ businessObject.getClass());
		}
	}
}
